package com.hall.controller;

import com.hall.pojo.Appointment;
import com.hall.pojo.AppointmentResult;
import com.hall.pojo.SysUser;
import com.hall.pojo.SysUserPermission;
import com.hall.service.AssistantService;
import com.hall.service.ScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
/**
 *用户列表
 */
@Controller
@RequestMapping("/all")
public class UserPermissionController {
    @Autowired
    private AssistantService assistantService;
    @Autowired
    private ScheduleService scheduleService;
    /*医助列表*/
    @RequestMapping("/assistantList")
    public String  assistantList(Model model){
       List<SysUser>list =  assistantService.allAssistantList();
       model.addAttribute("assistant",list);
       return "assistantList";
    }
    /*首页列表展示*/
    @ResponseBody
    @GetMapping("/doctorList")
    public List<SysUser> indexDoctor(){
        System.out.println(assistantService.allDoctor());
        return assistantService.allDoctor();
    }
    /*会诊添加专家的列表(查询的日程表)*//*暂时不用了*/
    @ResponseBody
    @GetMapping("/makeAppointmentDoctor")
    public AppointmentResult makeAppointmentDoctor(HttpServletRequest request){
       String menutype = request.getParameter("menutype");//添加汇报人还是专家
       String meetid   = request.getParameter("meetid"); //会议id获取
       String userid   = request.getParameter("userid");//当前用户id
       List<SysUser> sysUsers=new ArrayList<>();   //存所有有权限受约的专家集
       AppointmentResult result=new AppointmentResult(); //存最终结果集
       /*如果是添加汇报人,则直接查询有权限参与预约专家,然后返回,不参与日程表判断*/
       if("addexpert".equals(menutype)) {
           List<SysUser> meetDoctor = assistantService.meetDoctor();
           return new AppointmentResult(meetDoctor,meetid,menutype);//直接返回所有接受预约的专家
       }else{
       /*如果是添加专家,需要遍历所有有权限参与预约的专家,并且查询会议时间段,对应专家日程表*/
        List<SysUser> meetDoctor = assistantService.meetDoctor(); //所有有权限参与的专家
        Appointment ap = assistantService.meetTime(meetid);   // 查询对应会议号的本次会议开始结束时间
        /*遍历所有可预约的专家,查询日程是否冲突会议时间*/
        String apply = ap.getApplyTime().toString();  // xxxx-xx-xx xx:xx:xx
           for(SysUser s :meetDoctor){
           String sc= scheduleService.usertimeconfig(s.getId(),apply.substring(0,10));   //sc=当前遍历到的用户的当前会议时间的timeconfig
            if(sc==null ||"0000000000".equals(sc)){  //判断当前用户timeconfig和会议时间比较
                s.setExtprop("可约"); //如果该用户会议当天日程没有设置或者为0000000000则直接设置可约
                sysUsers.add(s);
            }
            //启用工具类
        }                                                        //遍历参会者查询日程表
        return null;
       }
    }
    /*查询所有用户权限列表*/
    @ResponseBody
    @GetMapping("/permission")
    public List<SysUserPermission> userPermission(){
       return assistantService.userPermission();
    }
}
